<?php
	function f240($sql){
		//data handle instance
		//set request
		//define format
		//set format
		//define query string
		//process time
		//process fitler
		$dataHandle = new DataHandle();
		$dataHandle->setRequest("240");

		$dataType = getValue($_GET['dataType']);
		$action = getValue($_GET['action']);

		if ($dataType == "json"){
			$format = array(
				"date" => "datem/d/Y",
			);

			$dataHandle->setFormat($format);
			$error = $dataHandle->processTime($startTime, $endTime);
			if($error == ""){
				$filter = $dataHandle->processFilters($startTime, $endTime, "lin_Technical_Changeovers","start_time");
				$query = getQuery($startTime, $endTime, $filter);
				print($dataHandle->printJSON($sql, $query));
			}
			else
				print($error);
		}
		else if($action == "getYears"){
			$query = getYears();
			print($dataHandle->printJSON($sql, $query));
		}
		else if($action == "getLines"){
			$query = getLines();
			print($dataHandle->printJSON($sql, $query));
		}
		else{
			$temp = array();
			$temp['error'] = "240: Bad request";
			print(json_encode($temp));
		}

		unset($dataHandle);

	}

	function getQuery(&$startTime, &$endTime, &$filter){
		$queryString = "
		SELECT 
			line_id,
			date, 
			shift,
			RIGHT('00000'+ CONVERT(VARCHAR,product_id_from),6) as product_id_from, 
			RIGHT('00000'+ CONVERT(VARCHAR,product_id_to),6) as product_id_to, 
			minutes
		FROM lin_Technical_Changeovers";

		$queryString = $queryString.$filter;

		$queryString = $queryString."
			ORDER BY line_id ASC, end_time ASC;";
		return $queryString;
	}
	function getYears(){
		$queryString = 
			"select distinct 
				datepart(year, modified_Date) as availableYear 
			from lin_Technical_Changeovers 
			order by availableYear desc";
		return $queryString;
	}
	function getLines(){
		$queryString = 
			"select distinct 
				line_id as availableLine 
			from lin_Technical_Changeovers 
			order by availableLine asc";
		return $queryString;
	}
?>